<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\LinkPager;

?>
<style>
    .table th:last-child{width: 32.94%;}
</style>
<div class="row">
    <div class="container lh30">
        <a class="topNav" href="<?=Url::toRoute('/system/site/index')?>"><img src="<?=Yii::getAlias('@web/backend/img/home/home.png');?>" alt="首页"/>返回首页</a>
    </div>
</div>

<div class="row nav-brand-bg borB">
    <div class="container lh30">
        <ul class="nav nav-tabs">
            <li class="active"><a href="" style="color: #575757;">供应商管理</a></li>
        </ul>
    </div>
</div>


<div class="container">
    <a class="btnBg text-center lh45 mg20-y add-supplier" href="javascript:;"><img src="<?=Yii::getAlias('@web/backend/img/supplier/add.png');?>">添加供应商</a>
    <div class="table-responsive">
        <?php if(empty($data)){?>
        <table class="table table-bordered ">
            <thead>
            <tr>
                <th>供应商名称</th>
                <th>负责人</th>
                <th>电话</th>
                <th>货款剩余</th>
                <th>赠送剩余</th>
                <th>供应商备注</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <tr class="setTr">
                    <td colspan="7" align="center"><img src="<?=Yii::getAlias('@web/backend/img/nodata.gif');?>"></td>
                </tr>
                <tr class="firstTr" style="display: none;">
                    <?php ActiveForm::begin(['action'=>Url::toRoute('supplier/add')])?>
                    <td><input  type="text" class="read" name="supplier_name" id="supplier_name"/></td>
                    <td><input type="text" name="supplier_contact" id="supplier_contact"  datatype="*"/></td>
                    <td><input type="text" name="supplier_phone" id="supplier_phone"  datatype="*"/></td>
                    <td><input type="text" name="supplier_balance" id="supplier_balance"  datatype="*"/></td>
                    <td><input type="text" name="supplier_give_balance" id="supplier_give_balance"  datatype="*"/></td>
                    <td><input type="text" name="supplier_remark" id="supplier_remark"  datatype="*"/></td>
                    <td><input type="submit" onclick="return chekTest()" value="保存" class="edit btn btn-sm btn-borB mr20" /><input type="reset" class="restb btn btn-sm btn-borB1" value="取消"></td>
                    <?php ActiveForm::end();?>
                </tr>
                <tr>
            </tbody>
        </table>
        <?php }else{?>
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>供应商名称</th>
                <th>负责人</th>
                <th>电话</th>
                <th>货款剩余</th>
                <th>赠送剩余</th>
                <th>供应商备注</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr class="firstTr" style="display: none;">
                    <?php ActiveForm::begin(['action'=>Url::toRoute('supplier/add')])?>
                        <td><input  type="text" class="read" name="supplier_name" id="supplier_name"/></td>
                        <td><input type="text" name="supplier_contact" id="supplier_contact"  datatype="*"/></td>
                        <td><input type="text" name="supplier_phone" id="supplier_phone"  datatype="*"/></td>
                        <td><input type="text" name="supplier_balance" id="supplier_balance"  datatype="*"/></td>
                        <td><input type="text" name="supplier_give_balance" id="supplier_give_balance"  datatype="*"/></td>
                        <td><input type="text" name="supplier_remark" id="supplier_remark"  datatype="*"/></td>
                        <td><input type="submit" onclick="return chekTest()" value="保存" class="edit btn btn-sm btn-borB mr10" /><input class="restb btn btn-sm btn-borB1" type="reset" value="取消"></td>
                    <?php ActiveForm::end();?>
                </tr>
            <?php foreach ($data as $d) {?>
            <tr>
            <?php ActiveForm::begin(['action'=>Url::toRoute('supplier/edit'),'options'=>['name'=>'supForm',]])?>
                <input class="supplier_id " type="hidden" name="supplier_id" value="<?=Html::encode($d['supplier_id']) ?>"/>
                <td><input class="repertory_name read-only supplier_name" type="text" name="supplier_name"     value="<?=Html::encode($d['supplier_name'])?>" readonly/></td>
                <td><input class="repertory_name read-only supplier_contact" type="text" name="supplier_contact"    value="<?=Html::encode($d['supplier_contact'])?>" readonly/></td>
                <td><input class="repertory_name read-only supplier_phone" type="text"  name="supplier_phone"   value="<?=Html::encode($d['supplier_phone'])?>" readonly/></td>
                <td><?=Html::encode($d['supplier_balance'])?></td>
                <td><?=Html::encode($d['supplier_give_balance'])?></td>
                <td><input class="repertory_name read-only supplier_remark" type="text" name="supplier_remark"    value="<?=Html::encode($d['supplier_remark'])?>" readonly/></td>
                <td>
                    <a class="money btn btn-sm btn-borB mr10" title="打款" durl="<?=Url::toRoute(['suppliefinance/payment','id' => $d['supplier_id']]) ?>" href="javascript:void(0)">打款</a>
                    <a class="money  btn btn-sm btn-borB1 mr10" title="扣款" durl="<?=Url::toRoute(['suppliefinance/deductmoney','id' => $d['supplier_id']]) ?>" href="javascript:void(0)">扣款</a>
                    <a class="money-log btn btn-sm btn-borB2 mr10" title="往来记录" durl="<?=Url::toRoute(['suppliefinance/list','id' => $d['supplier_id']]) ?>" href="javascript:void(0)">往来记录</a>
                    <a class="del btn btn-sm btn-borB3 mr10" did="<?=$d['supplier_id'] ?>" durl="<?=Url::toRoute("supplier/del") ?>" href="javascript:void(0)">作废</a>
                    <a class="edit btn btn-sm btn-borB4"  href="javascript:;">编辑</a>
                    <input class="tbit btn btn-sm btn-borB4 hidden cet" type="submit" value="保存"/>
                </td>
            <?php ActiveForm::end();?>
            </tr>
            <?php }?>
            </tbody>
        </table>
        <?php }?>
    </div>
    <div class="page">
        <?=LinkPager::widget([
            'pagination' => $page,
	       'firstPageLabel' => '首页',
	       'nextPageLabel' => '下一页',
	       'prevPageLabel' => '上一页',
	       'lastPageLabel' => '末页',
        ]); ?>
	</div>
</div>
<script>
    function chekTest(){
        var $supplier_id = $('#supplier_name').val();
            $supplier_contact = $('#supplier_contact').val();
            $supplier_phone = $('#supplier_phone').val();
            $supplier_balance = $('#supplier_balance').val();
            $supplier_give_balance = $('#supplier_give_balance').val();
            $supplier_remark = $('#supplier_remark').val();
        //验证用户名
        if ($supplier_id ==''){
            layer.alert('请填写供应商名称', {
                icon: 2,
                shade: 0.2,
            });
            return false
        }
        else if ($supplier_id.length > 30) {
            layer.alert('供应商名称不能超过30个字符', {
                icon: 2,
                shade: 0.2
            });
            return false
        }
        //验证负责人

        if ($supplier_contact.length > 5) {
            layer.alert('负责人名称不能超过5个字符', {
                icon: 2,
                shade: 0.2
            });
            return false
        }
        //验证手机号码
        if ($supplier_phone!='' && !/^1[3|4|5|8]\d{9}$/.test($supplier_phone) ) {
            layer.alert('请输入正确的手机号码', {
                icon: 2,
                shade: 0.2
            });
            return false
        }
        //验证贷款金额

        if ($supplier_balance!='' && !/^\d{0,6}$/.test($supplier_balance) ) {
            layer.alert('货款金额为数字类型且最长6位数', {
                icon: 2,
                shade: 0.2
            });
            return false
        }
        //验证赠送金额

        if ($supplier_give_balance!=''&& !/^\d{0,6}$/.test($supplier_give_balance) ) {
            layer.alert('赠送金额为数字类型且最长6位数', {
                icon: 2,
                shade: 0.2
            });
            return false
        }
        // 供应商备注
        if ($supplier_remark.length >30) {
            layer.alert('备注不能超过30个字符', {
                icon: 2,
                shade: 0.2
            });
            return false
        }
    }
    $(document).ready(function(){
        var $supplier_id = document.getElementsByClassName('supplier_name');
            $supplier_contact = document.getElementsByClassName('supplier_contact');
            $supplier_phone = document.getElementsByClassName('supplier_phone');
            $supplier_remark = document.getElementsByClassName('supplier_remark');
        var $subMit = document.getElementsByClassName('cet');
        for (var i = 0, j = $subMit.length; i < j; i++){
            //scope2 作用域2
            var $set = $subMit[i];
            $set.onclick = function(i){
                //scope3 作用域3
                return function(){
                    //scope4 作用域4
                    if ( i<$supplier_id.length){
                        //验证用户名
                        var $key = $supplier_id[i];
                        if ($key.value == '') {
                            layer.alert('请填写供应商名称', {
                                icon: 2,
                                shade: 0.2
                            });
                            return false
                        }
                        else if ($key.value.length > 30) {
                            layer.alert('供应商名称不能超过30个字符', {
                                icon: 2,
                                shade: 0.2
                            });
                            return false
                        }
                        //验证负责人
                        var $val = $supplier_contact[i];
                        if ($val.value.length > 5) {
                            layer.alert('负责人名称不能超过5个字符', {
                                icon: 2,
                                shade: 0.2
                            });
                            return false
                        }
                        //验证手机号码
                            var $phone = $supplier_phone[i];
                            if ($phone.value != '' && !/^1[3|4|5|8]\d{9}$/.test($phone.value)) {
                                layer.alert('请输入正确的手机号码', {
                                    icon: 2,
                                    shade: 0.2
                                });
                                return false
                            }
                        // 供应商备注
                            var $mes = $supplier_remark[i];
                            if ($mes.value.length > 30) {
                                layer.alert('备注不能超过30个字符', {
                                    icon: 2,
                                    shade: 0.2
                                });
                                return false
                            }

                    }
                }
            }(i)
        }
    });
</script>


<?php $this->registerJsFile("@web/system/supplier/list.js",['position'=>$this::POS_END]);?>
